package com.cloudbroker.bcs.platform.ltb.middle.bizservice.impl;

import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.HSBCPSErrorCodes;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.AdminRecommendRuleBizService;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.back.RecommendRuleBack;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.vo.AdminRecommendQryForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.vo.AdminSaveRecommendRuleForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.base.vo.HSLTBBaseVO;
import com.cloudbroker.bcs.platform.ltb.middle.entity.RecommendRule;
import com.cloudbroker.bcs.platform.ltb.middle.service.RecommendRuleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by world on 16/12/8.
 */
@Service
public class AdminRecommendRuleServiceImpl implements AdminRecommendRuleBizService {

    @Resource
    private RecommendRuleService recommendRuleService;

    public HSBCPSBackVO SaveRecommendRule(AdminSaveRecommendRuleForm form) {
        RecommendRule recommendRule = new RecommendRule();
        recommendRule.setCompany_id(form.getCompany_id());
        recommendRule.setRecommend_biz_type(form.getRecommend_biz_type());
        RecommendRule oldRecommendRule = recommendRuleService.getByEntity(recommendRule);
        if (oldRecommendRule == null) {
            recommendRule.setRecommend_order_type(form.getRecommend_order_type());
            recommendRule.setCreate_time(System.currentTimeMillis());
            recommendRule.setUpdate_time(System.currentTimeMillis());
            recommendRuleService.save(recommendRule);
        }else {
            oldRecommendRule.setRecommend_order_type(form.getRecommend_order_type());
            oldRecommendRule.setUpdate_time(System.currentTimeMillis());
            recommendRuleService.update(oldRecommendRule);
        }

        return VOUtil.setSuccessResult(new HSBCPSBackVO());
    }

    public RecommendRuleBack recommendRuleQry(AdminRecommendQryForm form) {
        RecommendRule recommendRule = new RecommendRule();
        recommendRule.setCompany_id(form.getCompany_id());
        recommendRule.setRecommend_biz_type(form.getRecommend_biz_type());
        recommendRule = recommendRuleService.getByEntity(recommendRule);
        if (recommendRule == null) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.PublicManageErrorInfo.RECOMMEND_RULE_NOT_EXISTS,RecommendRuleBack.class);
        }else {
            RecommendRuleBack ruleBack = new RecommendRuleBack();
            BeanUtils.copyProperties(recommendRule, ruleBack);
            return VOUtil.setSuccessResult(ruleBack);
        }
    }
}
